//
//  RHNetWorkTool.swift
//  微博新浪
//
//  Created by hezerong on 16/6/9.
//  Copyright © 2016年 Rocky. All rights reserved.
/*
App Key：125408973
App Secret：8416122382a6bd43b62b835d21051876
*/

import Foundation
import AFNetworking

enum RequestType:String{

   case GET = "GET"
   case POST = "POST"
}

let appkey = "125408973"
let appsecret = "8416122382a6bd43b62b835d21051876"
let directURL = "https://www.baidu.com"


class RHNetWorkTool: AFHTTPSessionManager {
    
    //网络中间层的单例
//    static let sharedTool = RHNetWorkTool()
    static let sharedTool:RHNetWorkTool = {
        let tool = RHNetWorkTool(baseURL: nil)
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool

    }()
    
    func request(url:String,
        method:RequestType,
        parameters:[String:AnyObject]?,
        callBack:([String:AnyObject])?->()){
    
            let success = { (task:NSURLSessionDataTask, response:AnyObject?) -> Void in
                
                if let response = response{
                
                    if let responseDic = response as? [String:AnyObject]{
                     
                        callBack(responseDic)
//                        print(responseDic)
                    }else{
                    
                       callBack(nil)
                    }
                
                }
            }
    
            let failure = { (task:NSURLSessionDataTask?, error:ErrorType) -> Void in
                print(error)
            }
            
            if method == .GET{
               
                GET(url, parameters: parameters,progress: nil, success: success, failure: failure)
            
                
            
            }
            
            if method == .POST {
                POST(url, parameters: parameters, progress: nil, success: success, failure: failure)
            }

            

    
    }

}

